--[[
 * Copyright (c) 2011 MoSync AB
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
--]]

----------------------------------------------------------------------
-- File: Hello.lua
-- Author: Mikael Kindborg
--
-- This programs shows how to write a MoSync Lua application.
-- The program displays an image and you can move the image 
-- if you have a touch sensitive display.
-- Pressing the back key or zero key exits the program.
----------------------------------------------------------------------

function Main()

	-- Variable for the image to display.
	local image = nil
	
	-- Start download of the image, download is asynchronous.
	System.downloadImage(
		"http://www.mosync.com/sites/all/themes/custom/mosync2/images/phones.png",
		-- This function will get called when the image has downloaded.
		function (imageHandle)
			-- If we get a non-nil value, download was successful.
			if nil ~= imageHandle then
				-- Here we scale the image, we use low-level MoSync
				-- function calls for this.
				-- Create a handle for the scaled image.
				image = maCreatePlaceholder()
				-- Scale the image to be twice as big.
				SysImageScaleProportionally(
					imageHandle, 
					nil, 
					image, 
					2.0, 
					SCALETYPE_BILINEAR)
				-- Deallocate the original unscaled image.
				maDestroyObject(imageHandle)
				-- Draw the image on the screen.
				DrawImage(image, 200, 200)
			end
		end)
	
	-- Set function to be called when a key is pressed.
	System.onKeyDown(function (key)
		if MAK_BACK == key or MAK_0 == key then
			maExit(0)
		end
	end)
	
	-- Set function to be called when the screen is touched.
	System.onTouchDown(function (x, y)
		DrawImage(image, x, y)
	end)
end

function DrawImage(image, x, y)

	-- First draw the background.
	Screen.setColor(34, 126, 255)
	Screen.fillRect(0, 0, Screen.getWidth(), Screen.getHeight())
	
	-- Then draw the image, if it has downloaded.
	if nil ~= image then
		local imageHalfWidth = Screen.getImageWidth(image) / 2
		local imageHalfHeight = Screen.getImageHeight(image) / 2
		Screen.drawImage(image, x - imageHalfWidth, y - imageHalfHeight)
	end
	
	-- Finally update the screen.
	Screen.update()
end

-- Start the program by calling the Main function.
-- This must be done as the last thing, after defining
-- the functions used by the program.
Main()